package hash;

import wrapper.Comparable;
import enumeration.Enumeration;
import main.LinkedList;

public class ChainedHashTable extends AbstractHashTable {
	protected LinkedList[] array;

	public ChainedHashTable(int length) {
		array = new LinkedList[length];
		for (int i = 0; i < length; ++i)
			array[i] = new LinkedList();
	}

	@Override
	public int getLength() {
		return array.length;
	}

	public void purge() {
		for (int i = 0; i < getLength(); ++i)
			array[i].purge();
		count = 0;
	}

	public void insert(Comparable object) {
		array[h(object)].append(object);
		++count;
	}

	public void withdraw(Comparable object) {
		array[h(object)].extract(object);
		--count;
	}

	public wrapper.Comparable find(wrapper.Comparable object) {
		for (LinkedList.Element ptr = array[h(object)].getHead(); ptr != null; ptr = ptr
				.getNext()) {
			wrapper.Comparable datum = (wrapper.Comparable) ptr.getDatum();
			if (object.isEQ(datum))
				return datum;
		}
		return null;
	}

	public void printHashTable() {
		for (int i = 0; i < getLength(); i++) {
			System.out.print("\n" + i + ": ");
			if (array[i] != null) {
				Enumeration enumeration = array[i].getEnumeration();
				while (enumeration.hasMoreElements()) {
					enumeration.nextElement();
				}
			}
		}
	}
}
